<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 导入mapbox依赖 -->
    <script src='https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css' rel='stylesheet' />
    <script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.2/mapbox-gl-draw.js"></script>
    <link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.2/mapbox-gl-draw.css"
        type="text/css">
    <script src="https://lib.baomitu.com/Turf.js/latest/turf.min.js"></script>
    <script src="https://lib.baomitu.com/jquery/2.2.4/jquery.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        #active {
            position: fixed;
            z-index: 1000;
            width: 200px;
            height: 50px;
            top: 10px;
            left: 50px;
        }
    </style>
</head>

<body>
    <button id="active" onclick="activeDraw()">激活画笔</button>
    <!-- 2、设置地图容器的挂载点 -->
    <div id="map">

    </div>
    <script>
        const gaode = {
            "version": 8,
            "sources": {
                "raster-tiles": {
                    "type": "raster",
                    "tiles": ["http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}"],
                    "tileSize": 256
                }
            },
            "layers": [{
                "id": "simple-tiles",
                "type": "raster",
                "source": "raster-tiles",
                "minzoom": 0,
                "maxzoom": 22
            }]
        }
        /* 3、实例化地图 */
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdiZW5jaGFvIiwiYSI6ImNsODU3aGRiODA0Y2UzcHBzZmFlcmdqZ2sifQ.8k59W_pB_Riwe6o-MneRlA'
        const map = new mapboxgl.Map({
            container: "map",
            /* style-layers */
            style: 'mapbox://styles/mapbox/streets-v12',
            center: [114.30, 30.50],
            zoom: 8
        })
        var draw = new MapboxDraw({
            //不允许使用键盘交互绘制
            keybindings: false,
            //设置为ture，可按住shift+拉框来拾取图形
            boxSelect: true,
            //点击要素时的响应半径（像素单位）
            clickBuffer: 5,
            //默认控件显示方式。如果设置为true，则会添加所有的绘图控件
            displayControlsDefault: false,
            //添加指定的绘制控件
            controls: {

            }
        });
        //将绘制控件添加到左上角
        map.addControl(draw, 'top-left');
        map.on("draw.create", handleDraw);
        function handleDraw(e) {
            // console.log(e);
            /* 获取面积 */
            var position = e.features[0]// geojson区数据
            // console.log(position);
            /* turf */
            var area = turf.area(position);// 求区域面积
            var center = turf.center(position).geometry.coordinates;// 求区域中心点
            console.log(center)  //经纬度转化屏幕

            let name = area / 1000 + "平方公里"
            // console.log(name)
            /* 自己造popup */
            /* 设置DOM */
            console.log(e)

        }
        function activeDraw() {
            console.log(draw);
            draw.changeMode("draw_polygon")
        }
    </script>
</body>

</html>